package com.gbco.intelligentpush.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.List;


@Slf4j
@Service
public class JedisAdapter implements InitializingBean {

    private JedisPool jedisPool;

    @Override
    public void afterPropertiesSet() throws Exception {

        JedisPoolConfig config = new JedisPoolConfig();
        //控制一个pool可分配多少个jedis实例，通过pool.getResource()来获取；
        //如果赋值为-1，则表示不限制；如果pool已经分配了maxActive个jedis实例，则此时pool的状态为exhausted(耗尽)。
        config.setMaxTotal(50);
        //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。
        config.setMaxIdle(5);
        //表示当borrow(引入)一个jedis实例时，最大的等待时间，如果超过等待时间，则直接抛出JedisConnectionException；单位毫秒
        //小于零:阻塞不确定的时间,  默认-1
        config.setMaxWaitMillis(1000*100);
        //在borrow(引入)一个jedis实例时，是否提前进行validate操作；如果为true，则得到的jedis实例均是可用的；
        config.setTestOnBorrow(true);
        //return 一个jedis实例给pool时，是否检查连接可用性（ping()）
        config.setTestOnReturn(true);
        //connectionTimeout 连接超时（默认2000ms）

        //soTimeout 响应超时（默认2000ms）
        jedisPool  = new JedisPool(config, "117.51.149.121", 6379,  2000, "6379");

        log.info("redis 连接成功");
    }


    public long lpush(String key,String message){
        Jedis resource = jedisPool.getResource();
        try{
            return resource.lpush(key,message);
        }catch (Exception e){
            log.error("jedis Exception : lpush "+ e);
            log.info("重新发起请求：findcard");
            try {
                Thread.sleep(1000);
                lpush(key,message);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }finally {
            if(resource!=null){
                resource.close();
            }
        }
        return 0;
    }

    public List<String> brpop(String key){
        Jedis resource = jedisPool.getResource();
        try{
            return resource.brpop(0,key);
        }catch (Exception e){
            log.error("jedis Exception : brpop "+ e);
        }finally {
            if(resource!=null){
                resource.close();
            }
        }
        return null;
    }

    public void set(String key , String value){
        Jedis resource = jedisPool.getResource();
        try{
            resource.set(key, value);
        }catch (Exception e){
            log.error("jedis Exception:"+e);
        }finally {
            if(resource!=null){
                resource.close();
            }
        }

    }

    public String get(String key){
        if(key!=null&&key!=""){
            Jedis resource = jedisPool.getResource();
            try{
                return resource.get(key);
            }catch (Exception e){
                log.error("jedis Exception:"+e);
            }finally {
                if(resource!=null){
                    resource.close();
                }
            }
        }
        return null;

    }

    public void setex(String key , int second , String value ){
        Jedis resource = jedisPool.getResource();
        try{
            resource.setex(key, second, value);
        }catch (Exception e){
            log.error("jedis Exception:"+e);
        }finally {
            if(resource!=null){
                resource.close();
            }
        }
    }

    public void setnx(String key , String value){
        Jedis resource = jedisPool.getResource();
        try{
            resource.setnx(key, value);
        }catch (Exception e){
            log.error("jedis Exception:"+e);
        }finally {
            if(resource!=null){
                resource.close();
            }
        }
    }

    public void rpush(String key , String value){
        Jedis resource = jedisPool.getResource();
        try{
            resource.rpush(key, value);
        }catch (Exception e){
            log.error("jedis Exception:"+e);
        }finally {
            if(resource!=null){
                resource.close();
            }
        }
    }

    public void lpop(String key ){
        Jedis resource = jedisPool.getResource();
        try{
            resource.lpop(key);
        }catch (Exception e){
            log.error("jedis Exception:"+e);
        }finally {
            if(resource!=null){
                resource.close();
            }
        }
    }

    public List<String> lrange(String key , int beging , int end){
        if(StringUtils.isNoneBlank(key)){
            Jedis resource = jedisPool.getResource();
            try{
                return resource.lrange(key,beging,end);
            }catch (Exception e){
                log.error("jedis Exception:"+e);
            }finally {
                if(resource!=null){
                    resource.close();
                }
            }
        }
        return null;
    }

    public void ltrim(String key , int begin , int end){
        Jedis resource = jedisPool.getResource();
        try{
            resource.ltrim(key,begin,end);
        }catch (Exception e){
            log.error("jedis Exception:"+e);
        }finally {
            if(resource!=null){
                resource.close();
            }
        }
    }

    public void lindex(String key , int index){
        Jedis resource = jedisPool.getResource();
        try{
            resource.lindex(key,index);
        }catch (Exception e){
            log.error("jedis Exception:"+e);
        }finally {
            if(resource!=null){
                resource.close();
            }
        }
    }

}
